
#import "HJCActionSheet.h"

@interface HJCActionSheet ()
{
    int _tag;
}

@property (nonatomic, weak) HJCActionSheet *actionSheet;

@property (nonatomic, weak) UIView *sheetView;

@end

@implementation HJCActionSheet

- (instancetype)initWithDelegateForShare:(id<HJCActionSheetDelegate>)delegate CancelTitle:(NSString *)cancelTitle
{
    
    HJCActionSheet *actionSheet = [self init];
    
    self.actionSheet = actionSheet;
    
    actionSheet.delegate = delegate;
    
    // 黑色遮盖
    actionSheet.frame = [UIScreen mainScreen].bounds;
    
    actionSheet.backgroundColor = [UIColor blackColor];
    
    [[UIApplication sharedApplication].keyWindow addSubview:actionSheet];
    
    actionSheet.alpha = 0.0;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick)];
    
    [actionSheet addGestureRecognizer:tap];
    
    // sheet
    UIView *sheetView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    sheetView.backgroundColor = [UIColor whiteColor];
    sheetView.alpha = 0.9;
    [[UIApplication sharedApplication].keyWindow addSubview:sheetView];
    self.sheetView = sheetView;
    sheetView.hidden = YES;
    
    _tag = 1;
    
    CGRect sheetViewF = sheetView.frame;
    sheetViewF.size.height = 44*2+BtnHeight;// BtnHeight * _tag + Margin
    sheetView.frame = sheetViewF;
    
    [self setupBtnForShare:@"微信"];
    
    [self setupBtnForShare:@"微信朋友圈"];
    
    // 取消按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, sheetView.frame.size.height - BtnHeight, ScreenWidth, BtnHeight)];
    [btn setTitle:cancelTitle forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.backgroundColor=[UIColor clearColor];
    btn.tag = 0;
    [btn addTarget:self action:@selector(sheetBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.sheetView addSubview:btn];
    
    //横线
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, (44*2)-1, DeviceWidth, 1)];
    view.backgroundColor=[UIColor lightGrayColor];
    view.alpha=0.5;
    [self.sheetView addSubview:view];
    
    return actionSheet;
    
}

- (instancetype)initWithDelegateAndArray:(id<HJCActionSheetDelegate>)delegate CancelTitle:(NSString *)cancelTitle  pageTitle:(NSString *)title  andFlag:(NSInteger)flag
{
    
    
    HJCActionSheet *actionSheet = [self init];
    
    self.actionSheet = actionSheet;
    
    actionSheet.delegate = delegate;
    
    // 黑色遮盖
    actionSheet.frame = [UIScreen mainScreen].bounds;
    
    actionSheet.backgroundColor = [UIColor blackColor];
    
    [[UIApplication sharedApplication].keyWindow addSubview:actionSheet];
    
    actionSheet.alpha = 0.0;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick)];
    
    [actionSheet addGestureRecognizer:tap];
    
    // sheet
    UIView *sheetView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    sheetView.backgroundColor = [UIColor blackColor];
    sheetView.alpha = 0.9;
    [[UIApplication sharedApplication].keyWindow addSubview:sheetView];
    self.sheetView = sheetView;
    sheetView.hidden = YES;
    
    _tag = 1;
    
    [self setupTableViewWith:delegate andFlag:flag];
    
    CGRect sheetViewF = sheetView.frame;
    if(flag==1)
    {
        sheetViewF.size.height = 44*8+BtnHeight*2;// BtnHeight * _tag + Margin;
    }
    else
    {
        sheetViewF.size.height = 44*3+BtnHeight*2;// BtnHeight * _tag + Margin
    }
    sheetView.frame = sheetViewF;
    
    // 取消按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, sheetView.frame.size.height - BtnHeight, ScreenWidth, BtnHeight)];
    //  [btn setBackgroundImage:normalImage forState:UIControlStateNormal];
    //  [btn setBackgroundImage:highImage forState:UIControlStateHighlighted];
    [btn setTitle:cancelTitle forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.backgroundColor=[UIColor clearColor];
    //btn.titleLabel.font = HeitiLight(17);
    btn.tag = 0;
    [btn addTarget:self action:@selector(sheetBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.sheetView addSubview:btn];
    
    // 播放顺序图标
    
    //    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
    //    img.image=[UIImage imageNamed:@"loop"];
    //    img.backgroundColor=[UIColor clearColor];
    //    [self.sheetView addSubview:img];
    
    //标题
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(DeviceWidth/2-(50), 10, 100, 31)];
    label.text=title;
    label.textAlignment= NSTextAlignmentCenter;
    label.backgroundColor=[UIColor clearColor];
    label.textColor=[UIColor whiteColor];
    [self.sheetView addSubview:label];
    
    //横线
    
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 45, DeviceWidth, 1)];
    view.backgroundColor=[UIColor whiteColor];
    view.alpha=0.5;
    [self.sheetView addSubview:view];
    
    return actionSheet;
    
}

- (instancetype)initWithDelegate:(id<HJCActionSheetDelegate>)delegate CancelTitle:(NSString *)cancelTitle OtherTitles:(NSString *)otherTitles, ...
{
    HJCActionSheet *actionSheet = [self init];
    
    self.actionSheet = actionSheet;
    
    actionSheet.delegate = delegate;
    
    // 黑色遮盖
    actionSheet.frame = [UIScreen mainScreen].bounds;
    actionSheet.backgroundColor = [UIColor blackColor];
    [[UIApplication sharedApplication].keyWindow addSubview:actionSheet];
    actionSheet.alpha = 0.0;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick)];
    [actionSheet addGestureRecognizer:tap];
    
    // sheet
    UIView *sheetView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    sheetView.backgroundColor = GlobelBgColor;
    sheetView.alpha = 0.9;
    [[UIApplication sharedApplication].keyWindow addSubview:sheetView];
    self.sheetView = sheetView;
    sheetView.hidden = YES;
    
    _tag = 1;
    
    NSString* curStr;
    va_list list;
    if(otherTitles)
    {
        [self setupBtnWithTitle:otherTitles];
        
        va_start(list, otherTitles);
        while ((curStr = va_arg(list, NSString*))) {
            [self setupBtnWithTitle:curStr];
            
        }
        va_end(list);
    }
    
    CGRect sheetViewF = sheetView.frame;
    sheetViewF.size.height =  ScreenHeight/2-20;// BtnHeight * _tag + Margin;
    sheetView.frame = sheetViewF;
    
    // 取消按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, sheetView.frame.size.height - BtnHeight, ScreenWidth, BtnHeight)];
    [btn setBackgroundImage:normalImage forState:UIControlStateNormal];
    [btn setBackgroundImage:highImage forState:UIControlStateHighlighted];
    [btn setTitle:cancelTitle forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = HeitiLight(17);
    btn.tag = 0;
    [btn addTarget:self action:@selector(sheetBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.sheetView addSubview:btn];
    
    return actionSheet;
}

- (void)show
{
    self.sheetView.hidden = NO;
    
    CGRect sheetViewF = self.sheetView.frame;
    sheetViewF.origin.y = ScreenHeight;
    self.sheetView.frame = sheetViewF;
    
    CGRect newSheetViewF = self.sheetView.frame;
    newSheetViewF.origin.y = ScreenHeight - self.sheetView.frame.size.height;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.sheetView.frame = newSheetViewF;
        
        self.actionSheet.alpha = 0.3;
    }];
}

- (void)setupBtnForShare:(NSString *)title
{
    
    CGFloat x=7+((_tag-1)*60)+((_tag-1)*7);
    CGFloat y=7;
    // 创建按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, 60, 60)];
//        [btn setBackgroundImage:normalImage forState:UIControlStateNormal];
//        [btn setBackgroundImage:highImage forState:UIControlStateHighlighted];
    
    [btn setBackgroundColor:[UIColor whiteColor]];
    if(_tag==1)
    {
        [btn setImage:[UIImage imageNamed:@"wx"] forState:UIControlStateNormal];
    }
    else
    {
        [btn setImage:[UIImage imageNamed:@"wq"] forState:UIControlStateNormal];
    }
    //[btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = HeitiLight(17);
    btn.tag = _tag;
    [btn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.sheetView addSubview:btn];
    
    //标题
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(x,y+60, 60, 20)];
    label.text=title;
    label.textAlignment= NSTextAlignmentCenter;
    label.backgroundColor=[UIColor clearColor];
    label.font = HeitiLight(12);
    label.textColor=[UIColor blackColor];
    [self.sheetView addSubview:label];
    
    _tag ++;
}


- (void)setupBtnWithTitle:(NSString *)title
{
    // 创建按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, BtnHeight * (_tag - 1) , ScreenWidth, BtnHeight)];
    [btn setBackgroundImage:normalImage forState:UIControlStateNormal];
    [btn setBackgroundImage:highImage forState:UIControlStateHighlighted];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = HeitiLight(17);
    btn.tag = _tag;
    [btn addTarget:self action:@selector(sheetBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.sheetView addSubview:btn];
    
    // 最上面画分割线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.5)];
    line.backgroundColor = GlobelSeparatorColor;
    [btn addSubview:line];
    
    _tag ++;
}

-(void)setupTableViewWith:(id) del andFlag:(NSInteger)flag
{
    CGFloat height=44*8;
    
    if(flag==2 || flag ==3)
    {
        height=44*3;
    }
    
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, BtnHeight , ScreenWidth,height)];
    tableView.delegate = del;
    tableView.dataSource= del;
    tableView.backgroundColor=[UIColor clearColor];
    
    [self.sheetView addSubview:tableView];
}

- (void)coverClick
{
    CGRect sheetViewF = self.sheetView.frame;
    sheetViewF.origin.y = ScreenHeight;
    
    [UIView animateWithDuration:0.2 animations:^{
        self.sheetView.frame = sheetViewF;
        self.actionSheet.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self.actionSheet removeFromSuperview];
        [self.sheetView removeFromSuperview];
    }];
}

- (void)sheetBtnClick:(UIButton *)btn{
    if (btn.tag == 0) {
        [self coverClick];
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
        [self.delegate actionSheet:self.actionSheet clickedButtonAtIndex:btn.tag];
        [self coverClick];
    }
}

- (void)shareBtnClick:(UIButton *)btn{
    if (btn.tag == 0) {
        [self coverClick];
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
        [self.delegate actionShareSheet:self.actionSheet clickedButtonAtIndex:btn.tag];
        [self coverClick];
    }
}

- (UIImage*)createImageWithColor:(UIColor*)color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
